package net.kesumu.general.android.opengl.object;

import java.util.List;

import android.graphics.Color;
import android.util.FloatMath;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.BASE_RADIUS;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.ITEM_BG_WIDTH;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.ITEM_FG_WIDTH;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.ITEM_BG_FG_WIDTH_DELT;
import net.kesumu.general.android.opengl.object.Geometry.Circle;
import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.Geometry.RingSector;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.ColorShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class YushabuMessageItem {
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int COLOR_COMPONENT_COUNT = 3;
    private static final int STRIDE = 
            (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    public static final int INITIATED = 0,
                            APPEARING = 1,
                            APPEARED = 2,
                            LEAVING = 3,
                            LEFT = 4;
    
    public float appearingRotateAngle = 85f;
    
    public final float startAngle = 275f;
    public final float endAngle = 360f;
    public int state;
    public Point circleCenter;
    
    public float itemNumber;
    public int color;
    public int numPoints;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;
    
    private final VertexArray leftVertexArray;
    private final List<DrawCommand> leftDrawList;

    public YushabuMessageItem(float itemNumber, int color, int numPoints) {
        this.state = INITIATED;
        this.itemNumber = itemNumber;
        
        float startAngleInRadians = (startAngle / 360f) * ((float) Math.PI * 2f);
        
        RingSector bgRingSector = new RingSector(new Point(0f, 0f, 0f), 
                BASE_RADIUS + itemNumber * ITEM_BG_WIDTH, 
                BASE_RADIUS + itemNumber * ITEM_BG_WIDTH + ITEM_BG_WIDTH, startAngle, endAngle);
        
        RingSector fgRingSector = new RingSector(new Point(0f, 0f, 0f), 
                BASE_RADIUS + itemNumber * ITEM_BG_WIDTH + ITEM_BG_FG_WIDTH_DELT, 
                BASE_RADIUS + itemNumber * ITEM_BG_WIDTH + ITEM_BG_FG_WIDTH_DELT + ITEM_FG_WIDTH, startAngle, endAngle);
        
        circleCenter = new Point(
                (BASE_RADIUS + itemNumber*ITEM_BG_WIDTH + ITEM_BG_WIDTH*0.5f) * FloatMath.cos(startAngleInRadians)
               ,(BASE_RADIUS + itemNumber*ITEM_BG_WIDTH + ITEM_BG_WIDTH*0.5f) * FloatMath.sin(startAngleInRadians)
               , 0f);
        
        Circle bgCircle = new Circle(circleCenter, ITEM_BG_WIDTH/2f);
        Circle fgCircle = new Circle(circleCenter, ITEM_FG_WIDTH/2f);
        
        GeneratedData generatedData = ObjectBuilder.createYushabuMessageItem(
                            bgRingSector, fgRingSector, bgCircle, fgCircle, color, numPoints);
        
        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
        
        
        int leftColor = Color.argb(64, (Color.red(color)+255+255)/3, (Color.green(color)+255+255)/3, (Color.blue(color)+255+255)/3);
        GeneratedData leftGeneratedData = ObjectBuilder.createYushabuMessageItem(
                bgRingSector, fgRingSector, bgCircle, fgCircle, leftColor, numPoints);
        
        leftVertexArray = new VertexArray(leftGeneratedData.vertexData);
        leftDrawList = leftGeneratedData.drawList;
    }
    
    public void bindData(ColorShaderProgram colorProgram) {
        vertexArray.setVertexAttribPointer(0,
            colorProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT, STRIDE);
        vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,
                colorProgram.getColorAttributeLocation(),
                COLOR_COMPONENT_COUNT, STRIDE);
    }
    
    public void leftBindData(ColorShaderProgram colorProgram) {
        leftVertexArray.setVertexAttribPointer(0,
            colorProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT, STRIDE);
        leftVertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,
                colorProgram.getColorAttributeLocation(),
                COLOR_COMPONENT_COUNT, STRIDE);
    }
    
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }
    
    public void leftDraw() {
        for (DrawCommand drawCommand : leftDrawList) {
            drawCommand.draw();
        }
    }

}
